package com.lepu.app.fun.grow.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;

import com.core.lib.utils.EmojiFilter;
import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;
import com.lepu.app.fun.grow.bean.PhotoItem;
import com.lepu.app.fun.grow.fragment.FragmentGrow;

public class PhotoItemAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	@SuppressWarnings("unused")
	private Context mContext = null;
	private ArrayList<PhotoItem> mArrayList = null;

	public PhotoItemAdapter(Context context, ArrayList<PhotoItem> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();

			// 强制性最多显示9张图片
			if (size >= FragmentGrow.PHOTO_COUNT_MAX) {
				size = FragmentGrow.PHOTO_COUNT_MAX;
			}
		}
		return size;
	}

	@Override
	public PhotoItem getItem(int position) {
		return mArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint({ "InflateParams", "DefaultLocale" })
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.grow_photo_listitem,
					null);

			holder = new ViewHolder();

			ImageView itemImageView = (ImageView) convertView
					.findViewById(R.id.itemImageView);
			EditText itemEditText = (EditText) convertView
					.findViewById(R.id.itemEditText);

			holder.itemImageView = itemImageView;
			holder.itemEditText = itemEditText;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final PhotoItem item = mArrayList.get(position);

		if (!TextUtils.isEmpty(item.imagePath)) {
			 UniversalImageLoadTool.disPlayNoCache(ImageUtilBase.getFileImageUrl(item.imagePath),
			 holder.itemImageView, 0);
		}
		holder.itemImageView.setTag(position);
		holder.itemImageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentGrow.getInstance().mModifyImageView = holder.itemImageView;
				FragmentGrow.getInstance().mModifyPhotoItem = item;
				FragmentGrow.getInstance().showPopAddPhoto();
			}
		});

		String itemEditTextTag = String.format("itemEditText_%d", position);
		holder.itemEditText.setTag(itemEditTextTag);

		// 阻止用户输入Emoji字符
		holder.itemEditText.setFilters(new InputFilter[] { new EmojiFilter(),
				new InputFilter.LengthFilter(50) });
		holder.itemEditText.setText(item.desc);

		return convertView;
	}

	private static class ViewHolder {
		ImageView itemImageView;
		EditText itemEditText;
	}

}
